package com.linran.javaweb.websocket;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author lin
 * @date 2020-08-26 22:58
 *
 * https://www.jianshu.com/p/3398d0230e5f
 * https://www.cnblogs.com/JohanChan/p/12522001.html
 */
@Controller
public class WebsocketController {

    @GetMapping("/websocket")
    public ModelAndView websocket(@RequestParam("userId") long userId) {
        System.out.println("转发" + userId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("websocket");
        modelAndView.addObject("userId", userId);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping("/sendMsg")
    public void sendMessage(long userId, String msg) {
        WebsocketDemo.sendMessageToUser(userId, msg);
    }
}
